<?php
/************************************************************************
Class Pick_Core_Module
----------------------------------------------------------------
PICKCOCMS Project
Company: PICKCO  Co., Ltd 
Name: Nguyen Hai Nam                                  
Last updated: 29/10/2009
**************************************************************************/
class Pick_Core_Module extends Pick_Core_Pick{
	// This field is information of module
	public $info;
	public $authentication = 0; # Default don't need auth
	
	function Pick_Core_Module() {
		global $opinfo;
		$this->info = $opinfo;
		// Initinal class
		parent::Pick_Core_Pick();
		
		// Check authenticaltion
		$this->checkAuthentication();
		
		// Load script and css default
		self::pushCSS("style.css");

		// Load data of plugins
		$strPlugins = "";
		if(is_object($this->info)){
			$strPlugins = $this->info->getField('plugin');
		}
		if($strPlugins!=""){
			$plugins = explode(",",$strPlugins);
			foreach($plugins as $val){
				$classPlugin = $val."Plugin";
				$objPlugin = new $classPlugin();
				$this->template->assign($val, $objPlugin->run());
				unset($objPlugin);	 
			}
			$this->template->assign("this", $this);	
		}
	}
	
	public function run(){
		$this->getHeader();
		$this->getContent();
		$this->getFooter();
		return $this->display();
	}
	// Set theme for module
	public function setTheme($theme){
		$this->theme = $theme;	
	}
	// Set layout for module
	public function setLayout($layout){
		$this->layout = $layout;	
	}
	// Check authentication for admin system
	public function checkAuthentication(){	
		if($this->authentication == 1){
			$users 		= new Pick_Core_User();
			$modules	= new Pick_Core_Modules();
			$logout = '';
			$userInfo = '';
			$objectModule = '';
			$objectModuleMenu = '';
			
			if($_SESSION['userId']) {
				$userId 	= $_SESSION['userId'];
				$userInfo 	= $users->getUserInfo($userId);
				if(is_object($userInfo)){
					$_SESSION['userType'] 	= $userInfo->getField('type');
					$arrayCodeModule 		= unserialize($userInfo->getField('permissions'));
					$objectModuleMenu 		= $modules->getArrayObjectsFromPermissionUser($userInfo);
					$arrayModule 			= $modules->getArrayModuleFromArrayCode(1, $arrayCodeModule, array('position'=>'ASC'));
					/* Kiem tra neu user dang nhap trai phep thi bat dang nhap lai */	
					if(!in_array($this->op, $arrayModule)){
						/* User truy cập thông tin trái phép */
						$logout = 'ok';			 
					}#endif
				}else{
					/* Thong tin user không tồn tại */
					$logout = 'ok';		
				}#endif
			} else {
				/* Chua dang nhap */
				$logout = 'ok';
			}#endif
			if($logout == 'ok'){
				redirect(base_url()."/login");
			}
			
			$this->template->assign('objectModule',$objectModuleMenu);
			$this->template->assign('authUser',$userInfo);	
			unset($objectModuleMenu);	
			unset($userInfo);			
		}
	}
	// Get content of header
	public function getHeader(){
		$this->getVariableForSmarty();
		//return $this->displayPart("header");
	}
	
	// Get content of content
	public function getContent(){}
	
	// Get content of footer
	public function getFooter(){
		//return $this->displayPart("footer");		
	}
	
    public function getArrayParentPath(){
        $modules = new Pick_Core_Modules();
        $objModule = $modules->getObjectFromCode($this->op);
        return $modules->getPathParrent($objModule->getField('id'));
    }
}
?>